package com.zmn.plat.admin.controller.product.tariff.group;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

import javax.annotation.Resource;

import com.zmn.common.utils.math.MathUtil;
import com.zmn.plat.manager.dictionary.MasterDict;
import com.zmn.plat.model.entity.product.tariff.tariffproductitem3.TariffProductItem3;
import com.zmn.plat.model.entity.product.tariff.tariffproductitem3.TariffProductItem3Query;
import com.zmn.plat.services.interfaces.product.tariff.tariffproductitem3.TariffProductItem3Service;
import org.apache.shiro.util.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.servlet.ModelAndView;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.plat.admin.controller.PlatBaseController;
import com.zmn.plat.business.interfaces.product.tariff.group.ProductTariffGroupBService;
import com.zmn.plat.manager.dictionary.ProductDict;
import com.zmn.plat.model.entity.product.ServProduct;
import com.zmn.plat.model.entity.product.tariff.ProductTariff;
import com.zmn.plat.model.entity.product.tariff.ProductTariffQuery;
import com.zmn.plat.model.entity.product.tariff.group.ProductTariffGroupQuery;
import com.zmn.plat.model.entity.service.tariff.ServiceTariff;
import com.zmn.plat.model.vo.product.tariff.ProductTariffVO;
import com.zmn.plat.model.vo.product.tariff.group.ProductTariffGroupVO;
import com.zmn.plat.model.vo.product.tariff.item.ProductTariffGroupItemVo;
import com.zmn.plat.services.interfaces.product.ServProductService;
import com.zmn.plat.services.interfaces.product.tariff.ProductTariffService;
import com.zmn.plat.services.interfaces.service.tariff.ServiceTariffService;

/**
 * 产品故障分组表
 *
 * @author linfeng
 * @since 2019-11-21 18:57:24
 */
public class ProductTariffGroupBaseController extends PlatBaseController {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    protected ProductTariffGroupBService productFaultGroupBService;
    @Resource
    protected ServiceTariffService faultTariffService;
    @Resource
    protected ServProductService servProductService;
    @Resource
    protected ProductTariffService productFaultTariffService;
    @Resource
    protected TariffProductItem3Service tariffProductItem3Service;

    private String narrow(Object dividend) {
        if (Objects.isNull(dividend)) {
            return "";
        }
        double div = MathUtil.div(Integer.parseInt(dividend.toString()), MasterDict.COEFFICIENT_MAGNIFICATION, 2);
        BigDecimal b = new BigDecimal(div);
        return b.setScale(2, BigDecimal.ROUND_HALF_UP).toString();
    }
    @SuppressWarnings("Duplicates")
    ModelAndView list(@ModelAttribute ProductTariffGroupQuery query) {
        ServiceTariff faultTariff = faultTariffService.findByKey(query.getTariffId());
        ProductTariffQuery productFaultTariffQuery = new ProductTariffQuery();
        productFaultTariffQuery.setProductId(query.getProductId());
        productFaultTariffQuery.setTariffId(query.getTariffId());
        List<ProductTariffVO> productFaultTariffs = productFaultTariffService.listByQuery(productFaultTariffQuery);
        ProductTariff productFaultTariff = new ProductTariff();
        if (productFaultTariffs != null && productFaultTariffs.size() > 0) {
            BeanUtils.copyProperties(productFaultTariffs.get(0), productFaultTariff);
        }

        ServProduct product =
            query.getProductId() > ProductDict.ZERO ? servProductService.findByKey(query.getProductId()) : new ServProduct();
        // 是否显示全部项目：如果该产品价格表当前没有勾选项目，默认显示全部项目； 如果已有勾选项目，默认不显示全部项目
        boolean isShowAllItems = true;
        List<ProductTariffGroupVO> productFaultGroupVOList = productFaultGroupBService.listGroupVOByQuery(query);
        if (!CollectionUtils.isEmpty(productFaultGroupVOList)) {
            for (ProductTariffGroupVO productFaultGroupVO : productFaultGroupVOList) {
                if (!CollectionUtils.isEmpty(productFaultGroupVO.getItems())) {
                    if (faultTariff.getTariffType() == 4) { // 加载计算器3.0的基础价格
                        for (ProductTariffGroupItemVo vo : productFaultGroupVO.getItems()) {
                            TariffProductItem3Query tariffProductItem3Query = new TariffProductItem3Query();
                            tariffProductItem3Query.setItemId(vo.getItemId());
                            tariffProductItem3Query.setGroupId(vo.getGroupId());
                            tariffProductItem3Query.setTariffId(vo.getTariffId());
                            tariffProductItem3Query.setProductId(productFaultGroupVO.getProductId());
                            List<TariffProductItem3> items = tariffProductItem3Service.listByQuery(tariffProductItem3Query);
                            if (items!=null && items.size()>0){
                                TariffProductItem3 item = items.get(0);

                                vo.setPrice(item.getHourFee()+item.getTrafficFee()+item.getPartFee());
                                vo.setPriceYuan(narrow(vo.getPrice()));


                                vo.setInternalSettlementPriceYuan(narrow(item.getInternalSettlementPrice()));
                                vo.setExternalSettlementPriceYuan(narrow(item.getExternalSettlementPrice()));

                                vo.setHourFee(item.getHourFee());
                                vo.setHourFeeYuan(narrow(vo.getHourFee()));

                                vo.setTrafficFee(item.getTrafficFee());
                                vo.setTrafficFeeYuan(narrow(vo.getTrafficFee()));

                                vo.setPartPrice(item.getPartFee());
                                vo.setPartPriceYuan(narrow(vo.getPartPrice()));
                            }else{
                                vo.setInternalSettlementPriceYuan("");
                                vo.setExternalSettlementPriceYuan("");
                            }
                        }
                    }

                    for (ProductTariffGroupItemVo productFaultItemVo : productFaultGroupVO.getItems()) {
                        if (productFaultItemVo.getStatus() != null && productFaultItemVo.getStatus().equals(GlobalConsts.YES)) {
                            isShowAllItems = false;
                            break;
                        }
                    }

                }
            }
        }

        ModelAndView mav = new ModelAndView("product/service/group/list");
        mav.addObject("productFaultTariff", productFaultTariff);
        mav.addObject("productId", query.getProductId());
        mav.addObject("servProductVO", product);
        mav.addObject("isShowAllItems", isShowAllItems);
        mav.addObject("bizType", faultTariff.getBizType());
        mav.addObject("productFaultGroupVOList", productFaultGroupVOList);
        mav.addObject("tariffId", query.getTariffId());
        mav.addObject("faultTariff", faultTariff);
        mav.addObject("general", faultTariff == null ? "" : faultTariff.getGeneral());

        return mav;
    }
}
